package cn.edu.seu.ssny.common;

import cn.edu.seu.ssny.enums.ResultCodeEnum;
import lombok.Data;

import java.util.HashMap;
import java.util.Map;

@Data
public class R {
    /**
     * 响应是否成功
     */
    private boolean success;
    /**
     * 响应状态码
     */
    private int code;

    /**
     * 状态码描述
     */
    private String message;

    /**
     * 响应数据
     */
    private Map<String, Object> data = new HashMap<>();

    /**
     * 构造器私有
     */
    private R() {

    }

    /**
     * 通用返回失败，未知错误
     *
     * @return
     */
    public static R error() {
        R r = new R();
        r.setSuccess(ResultCodeEnum.ERROR_UNKOWN.isSuccess());
        r.setCode(ResultCodeEnum.ERROR_UNKOWN.getCode());
        r.setMessage(ResultCodeEnum.ERROR_UNKOWN.getMessage());
        return r;
    }

    /**
     * 通用成功
     *
     * @return
     */
    public static R ok() {
        R r = new R();
        r.setSuccess(ResultCodeEnum.SUCCESS.isSuccess());
        r.setCode(ResultCodeEnum.SUCCESS.getCode());
        r.setMessage(ResultCodeEnum.SUCCESS.getMessage());
        return r;
    }

    /**
     * 通过定义好的结果枚举设置结果
     *
     * @param resultCodeEnum
     * @return
     */
    public static R result(ResultCodeEnum resultCodeEnum) {
        R r = new R();
        r.setSuccess(resultCodeEnum.isSuccess());
        r.setCode(resultCodeEnum.getCode());
        r.setMessage(resultCodeEnum.getMessage());
        return r;
    }


    /**
     * 链式编程 设置结果
     * @param map
     * @return
     */
    public R data(Map<String, Object> map) {
        this.setData(map);
        return this;
    }

    /**
     * 链式编程，设置状态信息
     * @param message
     * @return
     */
    public R message(String message) {
        this.setMessage(message);
        return this;
    }

    /**
     * 链式编程，设置状态码
     * @param code
     * @return
     */
    public R code(int code) {
        this.code = code;
        return this;
    }

    /**
     * 链式编程，设置是否成功
     * @param success
     * @return
     */
    public R success(boolean success) {
        this.success = success;
        return this;
    }


}
